﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Drawing;
using System.Resources;

namespace Brewmaster
{
    class LocalizationManager
    {

        /// <summary>
        /// Contains a list of supported languages' LCIDs
        /// </summary>
        public readonly String[] CULTURE_CODE = { "en", "hr" };

        /// <summary>
        /// Contains a list of supported languages' names
        /// </summary>
        public readonly String[] CULTURE_NAME = { "English", "Hrvatski" };

        /// <summary>
        /// Sets the default language in case of empty settings file
        /// </summary>
        public const int CULTURE_DEFAULT = 0;

        /// <summary>
        /// The resource to be used as the image associated with a language should have
        /// a name consisting of the culture's LCID followed by this suffix
        /// </summary>
        public const String CULTURE_IMAGE_SUFFIX = "_img";

        private MainForm main;

        public LocalizationManager(MainForm whichMain)
        {
            main = whichMain;
        }

        public void localize(MainForm whichMain)
        {
            if (Brewmaster.Properties.Settings.Default.Language == "")
            {
                Brewmaster.Properties.Settings.Default.Language = CULTURE_CODE[CULTURE_DEFAULT];
            }
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(Brewmaster.Properties.Settings.Default.Language);
            main.localize();
        }

        public ContextMenuStrip getLocalizationMenu()
        {
            ContextMenuStrip menu = new ContextMenuStrip();
            for (int i = 0; i < CULTURE_CODE.Length; i++)
            {
                try
                {
                    menu.Items.Add(CULTURE_NAME[i], (Image)Brewmaster.Properties.Images.ResourceManager.GetObject(CULTURE_CODE + CULTURE_IMAGE_SUFFIX), new EventHandler(LanguageMenuItem_Click));
                }
                catch (MissingManifestResourceException)
                {
                }
            }
            return menu;
        }

        private void LanguageMenuItem_Click(object sender, EventArgs e)
        {
            int index = -1;
            for (int i = 0; i < CULTURE_CODE.Length; i++){
                if (CULTURE_NAME[i] == ((ToolStripItem)sender).Text){
                    index = i;
                    break;
                }
            }
            if (index != -1){
                Brewmaster.Properties.Settings.Default.Language = CULTURE_CODE[index];
                Brewmaster.Properties.Settings.Default.Save();
            }
            localize(main);
        }
    }
}
